Grokking Oracle Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Oracle Easy
Count Number of Pairs With Absolute Difference K (easy)
Solution: Count Number of Pairs With Absolute Difference K
Degree of an Array (easy)
Solution: Degree of an Array
Find Subsequence of Length K With the Largest Sum (easy)
Solution: Find Subsequence of Length K With the Largest Sum
Rectangle Overlap (easy)
Solution: Rectangle Overlap
Remove All Adjacent Duplicates In String (easy)
Solution: Remove All Adjacent Duplicates In String
Island Perimeter (easy)
Solution: Island Perimeter
Climbing Stairs (easy)
Solution: Climbing Stairs
Move Zeroes (easy)
Solution: Move Zeroes
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Valid Parentheses (Easy)
Solution: Valid Parentheses
Oracle Medium
Longest Palindromic Substring (medium)
Solution: Longest Palindromic Substring
Single Element in a Sorted Array (medium)
Solution: Single Element in a Sorted Array
Pairs of Songs With Total Durations Divisible by 60 (medium)
Solution: Pairs of Songs With Total Durations Divisible by 60
Search a 2D Matrix II (medium)
Solution: Search a 2D Matrix II
Minimize Maximum of Array (medium)
Solution: Minimize Maximum of Array
Asteroid Collision (medium)
Solution: Asteroid Collision
Combination Sum (medium)
Solution: Combination Sum
Binary Tree Right Side View (medium)
Solution: Binary Tree Right Side View
Sort Colors (medium)
Solution: Sort Colors
Group Anagrams (Medium)
Solution: Group Anagrams
Decode String (Medium)
Solution: Decode String
Number of Islands (medium)
Solution: Number of Islands
Binary Tree Zigzag Level Order Traversal (medium)
Solution: Binary Tree Zigzag Level Order Traversal
Longest Common Subsequence (medium)
Solution: Longest Common Subsequence
Rotting Oranges (medium)
Solution: Rotting Oranges
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Spiral Matrix (medium)
Solution: Spiral Matrix
Container With Most Water (Medium)
Solution: Container With Most Water
3Sum (medium)
Solution: 3Sum
Simplify Path (medium)
Solution: Simplify Path
Next Permutation (medium)
Solution: Next Permutation
Basic Calculator II (medium)
Solution: Basic Calculator II
Merge Intervals (medium)
Solution: Merge Intervals
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Validate Binary Search Tree (medium)
Solution: Validate Binary Search Tree
Oracle Hard
Integer to English Words (hard)
Solution: Integer to English Words
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Merge K Sorted Lists (hard)
Solution: Merge K Sorted Lists
Max Points on a Line (hard)
Solution: Max Points on a Line
Sudoku Solver (hard)
Solution: Sudoku Solver
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Solution: Integer to English Words

Problem Statement

Given a positive integer num, return the English word representation of num.

Examples

  • Example 1:

    • Input: num = 12345
    • Expected Output: "Twelve Thousand Three Hundred Forty Five"
    • Justification: The number 12345 is broken down into "Twelve Thousand" for the first two digits, followed by "Three Hundred" for the next digit, and "Forty Five" for the last two digits.
  • Example 2:

    • Input: num = 1001
    • Expected Output: "One Thousand One"
    • Justification: The number 1001 translates to "One Thousand" for the first digit and "One" for the last digit, skipping the hundreds and tens place because they are zeros.
  • Example 3:

    • Input: num = 2000000
    • Expected Output: "Two Million"
    • Justification: The number 2000000 is simply "Two Million" as there are no other numbers to represent after the word "Million".

Solution

To solve this problem, the approach involves breaking down the given number into manageable chunks, each representing a part of the number in words. This method is effective because it mirrors how humans naturally interpret numbers. By dividing the number into segments such as billions, millions, thousands, and then the last three digits, we can systematically convert each segment into words using predefined mappings for numbers 1-19, tens (20, 30, ..., 90), and the significant place values (thousand, million, billion). This approach ensures that we can handle any non-negative integer input systematically and efficiently.

The algorithm's effectiveness lies in its simplicity and scalability. By treating the number in segments, we can reuse the same logic for each segment, making the code more maintainable and easier to understand. This method also scales well for large numbers, as it applies the same logic regardless of the number's size, ensuring consistency in the output.

Step-by-Step Algorithm

  1. Initialization: Define three arrays or lists for mapping numbers to words. These include:

    • lessThan20 for numbers 1 to 19.
    • tens for multiples of ten from 20 to 90.
    • thousands for the place values thousand, million, and billion.
  2. Check for Zero: If the input number is 0, immediately return the word "Zero".

  3. Conversion Process:

    • Initialize an empty string words to hold the final words representation.
    • Initialize a counter i to keep track of the current place value (thousands, millions, etc.).
    • While the input number is greater than 0, perform the following steps:
      • If the last three digits of the current number segment (num % 1000) are not zero, convert this segment to words using a helper function and append the appropriate place value from the thousands array.
      • Divide the number by 1000 to move to the next segment.
      • Increment the place value counter i.
  4. Helper Function:

    • This function takes a number less than 1000 and converts it into words.
    • If the number is less than 20, return the corresponding word from lessThan20.
    • If the number is between 20 and 99, return the word for the tens place from tens and recursively call the helper function for the remainder (units place).
    • If the number is between 100 and 999, return the word for the hundreds place from lessThan20, the word "Hundred", and recursively call the helper function for the remainder (less than 100).
  5. Finalization:

    • Trim any leading or trailing spaces from the words string.
    • Return the words string as the final English words representation of the input number.

Algorithm Walkthrough

Let's consider the input: num = "12345"

  1. Initialization: The mappings for numbers to words are defined in arrays/lists as per the algorithm.

  2. Check for Zero: The input number is 12345, which is not zero, so we proceed.

  3. Conversion Process:

    • The input number 12345 is greater than 0, so we start the conversion.
    • First Iteration:
      • The last three digits are 345. Next, we convert 345 to words.
      • We call the helper function with 345.
        • The number is greater than 100, so we get "Three Hundred " + helper(45).
        • For 45, it falls in the less than 100 category. So, we get "Forty " + helper(45%10).
        • For 5, it falls in the less than 20 category. So, we get "Five".
        • The final return for this segment is "Three Hundred Forty Five".
      • We append "" (empty string) after converting 345 to words because i = 0 (first segment after the units).
      • The words string remains same "Three Hundred Forty Five ".
      • We divide the number by 1000, updating it to 12.
    • Second Iteration:
      • Now, the number is 12.
      • Since 12 % 1000 = 12 is not zero, we convert 12 to words using the helper function.
        • 12 is less than 20, so we directly get "Twelve" from lessThan20.
      • Prepand words string with thousands[1] and helper(12).
      • The words string is updated to "Twelve Thousand Three Hundred Forty Five ".
    • The loop ends as the number is now 0.
  4. Finalization:

    • Trim the final words string to remove any trailing space.
    • The final output is "Twelve Thousand Three Hundred Forty Five".

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The algorithm has a linear time complexity relative to the number of digits in the input number. In the worst case, it processes each group of three digits once, leading to a time complexity of , where n is the number of digits.

Space Complexity

The space complexity is for the storage of mappings (lessThan20, tens, thousands), which does not scale with the input size. However, the recursive calls in the helper function use stack space, but since the depth is limited to a maximum of 3 (for numbers less than 1000), it can also be considered .

Integer to English Words (hard)
Trapping Rain Water (hard)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity